package com.tjzx.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tjzx.entity.EipQuestionInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * EipQuestionInfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.tjzx.entity.EipQuestionInfo
 * @author MyEclipse Persistence Tools
 */

public class EipQuestionInfoDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(EipQuestionInfoDAO.class);

	// property constants

	protected void initDao() {
		// do nothing
	}

	public void save(EipQuestionInfo transientInstance) {
		log.debug("saving EipQuestionInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(EipQuestionInfo persistentInstance) {
		log.debug("deleting EipQuestionInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public EipQuestionInfo findById(java.lang.Integer id) {
		log.debug("getting EipQuestionInfo instance with id: " + id);
		try {
			EipQuestionInfo instance = (EipQuestionInfo) getHibernateTemplate()
					.get("com.tjzx.entity.EipQuestionInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public int findMaxId(DetachedCriteria criteria) {
		Criteria criteria1 = criteria.getExecutableCriteria(this.getSession());

        int maxid = (Integer) criteria1.setProjection(Projections.projectionList().add(Projections.max("qid"))).uniqueResult();
        criteria.setProjection(null);
        
        return maxid;
	}
	
	public int findTotalRows(DetachedCriteria criteria) {
		Criteria criteria1 = criteria.getExecutableCriteria(this.getSession());

        int totalCount = (Integer) criteria1.setProjection(Projections.rowCount()).uniqueResult();
        criteria.setProjection(null);
        
        return totalCount;
	}
	
	 public Integer getCount(final DetachedCriteria detachedCriteria) {
         return (Integer) getHibernateTemplate().executeWithNativeSession(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = detachedCriteria.getExecutableCriteria(session);

                int totalCount = (Integer) criteria.setProjection(
                        Projections.rowCount()).uniqueResult();
                criteria.setProjection(null);
                return totalCount;
			}
         });
     }


	
	public List findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) {
		log.debug("finding EipQuestionInfo instance with property: " + criteria);
		try {
			this.getSession();
			return this.getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByExample(EipQuestionInfo instance) {
		log.debug("finding EipQuestionInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding EipQuestionInfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from EipQuestionInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all EipQuestionInfo instances");
		try {
			String queryString = "from EipQuestionInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public EipQuestionInfo merge(EipQuestionInfo detachedInstance) {
		log.debug("merging EipQuestionInfo instance");
		try {
			EipQuestionInfo result = (EipQuestionInfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(EipQuestionInfo instance) {
		log.debug("attaching dirty EipQuestionInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(EipQuestionInfo instance) {
		log.debug("attaching clean EipQuestionInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static EipQuestionInfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (EipQuestionInfoDAO) ctx.getBean("EipQuestionInfoDAO");
	}
}